<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../layui/css/layui.css"  media="all">

  <style>
    .cate_list {
      list-style: none;
      padding: 10px;
    }

    .cate_list .cate_item {
      margin: 2px;
      height: 36px;
      line-height: 36px;
      padding: 0 15px;
      background-color: #009688;
      color: #f2f2f2;
      border-radius: 2px;
      font-size: 14px;
    }
  </style>
</head>
<body>
           
<button class="layui-btn layui-btn-sm layui-btn-normal" id="btnAdd" >添加分类</button>
<button class="layui-btn layui-btn-sm" id="btnSort" >分类排序</button>

<table class="layui-hide" id="cate" lay-filter="cate"></table>

<ul class="cate_list" id="cate_sortable" style="display: none;">
  <li class="cate_item">1 java</li>
  <li class="cate_item">2 c++</li>
  <li class="cate_item">3 合集模式</li>
  <li class="cate_item">4 算法基础</li>
  <li class="cate_item">4 算法基础</li>
</ul>

<div id="categories-form" style="display: none;padding:20px;"> 
  <label >分类名</label>
  <div class="layui-input-inline">
    <input type="text" name="categoryName" class="layui-input">
  </div>
</div>

<script src="../layui/layui.js" charset="utf-8"></script>
<script src="../vendor/html5sortable.min.js"></script>


<script>

sortable('#cate_sortable', {
  forcePlaceholderSize: true,
  placeholderClass: 'mb1 bg-navy border border-yellow',
  hoverClass: 'bg-maroon yellow'
});

var data = [
  {
    "id":1,
    "name": "java",
    "priority": 1
  },
  {
    "id":2,
    "name": "c++",
    "priority": 2
  },
  {
    "id":3,
    "name": "javascrit",
    "priority": 3
  }
];

layui.use(['table', 'layer'], function() {
  var table = layui.table;

  table.render({
    elem: '#cate',
    cols: [[ //标题栏
      {checkbox: true},
      {field: 'id', title: 'ID', width: 100},
      {field: 'priority', title: '优先级', width: 120, edit: 'text'},
      {field: 'name', title: '名字', minwidth: 200, edit: 'text'}
    ]],
    data: data
  });

  //监听单元格编辑
  table.on('edit(cate)', function(elem) {
    var field = elem.field; //得到字段
    var value = elem.value; //得到修改后的值
    var data = elem.data; //得到所在行所有键值

    layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
  });

  var $ = layui.jquery;
  
  $('#btnAdd').on('click', function() {
	layer.open({
		type: 1
		,title: '添加分类'
		,closeBtn: false
		,shade: 0.0
		,id: 'categories-add-form' //设定一个id，防止重复弹出
		,btn: ['保存', '关闭']
		,btnAlign: 'r'
		,moveType: 1 //拖拽模式，0或者1
		,content: $('#categories-form')
		,success: function(layero){
		  var btn = layero.find('.layui-layer-btn');
		  btn.find('.layui-layer-btn0').on('click', function() {
			var name = layero.find('input[name="categoryName"]').val();
			alert(name);
		  });
		}
	  }); 
  });
  
  $('#btnSort').on('click', function(){
    layer.open({
        type: 1
        ,title: '拖拽分类来重新排序'
        ,closeBtn: false
        ,area: '250px;'
        ,shade: 0.0
        ,id: 'category_sort' //设定一个id，防止重复弹出
        ,btn: ['保存', '关闭']
        ,btnAlign: 'r'
        ,moveType: 1 //拖拽模式，0或者1
        ,content: $('#cate_sortable')
        ,success: function(layero){
          var btn = layero.find('.layui-layer-btn');
          btn.find('.layui-layer-btn0').on('click', function() {
        	  alert(name);
          });
        }
      });
  });
});
</script>

</body>
</html>